/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var play = false;

function togglePlay() {
    if(!play) {
        projekktor('.projekktor').setPlay();
        play = true;
    }
    else {
        projekktor('.projekktor').setStop();
        play = false;
    }
}

// Add a song to a Speakker playlist and play
function addSongToPlayListAndPlay(songId, path, title, coverArt) {
    var item = {
        0: {
            src: path, 
            type: 'audio/mp3'
        },
        config: {
            title: title,
            cover: coverArt
        }
    };
    
    // Get the current playlist to verify if the song is already added into it
    var playList = projekktor('.projekktor').getItem('*');
    var countItems = 0;
    console.log("########");
    for(var i in playList) {
        var playlist_file = projekktor('.projekktor').getItem(i).file;
        if(playlist_file) {
            console.log(playlist_file[0].src);
            
            if(endsWith(playlist_file[0].src, path)) {
                projekktor('.projekktor').setActiveItem(countItems);
                return;
            }
        }
        countItems ++;
    }
    console.log("########");
    projekktor('.projekktor').setItem(item, 0, false);
    
    updateNbPlayedSong(songId);
}

// Add a song to a Speakker playlist and don't play it
function addSongToPlayList(songId, path, title, coverArt) {
    var item = {
        0: {
            src: path, 
            type: 'audio/mp3'
        },
        config: {
            title: title,
            cover: coverArt
        }
    };
    
    // Get the current playlist to verify if the song is already added into it
    var playList = projekktor('.projekktor').getItem('*');
    var countItems = 0;
    console.log("########");
    for(var i in playList) {
        var playlist_file = projekktor('.projekktor').getItem(i).file;
        if(playlist_file) {
            console.log(playlist_file[0].src);
            
            if(endsWith(playlist_file[0].src, path)) {
                projekktor('.projekktor').setActiveItem(countItems);
                return;
            }
        }
        countItems ++;
    }
    console.log("########");
    projekktor('.projekktor').setItem(item, countItems, false);
    
    updateNbPlayedSong(songId);
}

// Set the active playlist and play
function playPlayList(playlistId, playlist) {
    var obj = jQuery.parseJSON(playlist);
    console.log("Playlist chargee : " + playlist);
    projekktor('.projekktor').setFile(obj);
    projekktor('.projekktor').setPlay();
    
    updatePlayCounterPlaylist(playlistId);
}

function playSongFromPlayList(songId, song, playlist) {
    var obj = jQuery.parseJSON(playlist);
    console.log("Playlist chargee : " + playlist);
    projekktor('.projekktor').setFile(obj);
    projekktor('.projekktor').setActiveItem(song);
    projekktor('.projekktor').setPlay();
    
    updateNbPlayedSong(songId);
}

// Veryfy if a string ends with another string
function endsWith(str, suffix) {
    return str.indexOf(suffix, str.length - suffix.length) !== -1;
}

// Reinit the player
function reinit() {
    projekktor('.projekktor').reset();
}
/*
// Save the current playlist
function savePlaylist() {
    var playlist = projekktor('.projekktor').getPlaylist();
    
    console.log(playlist);
    
    $.ajax({
        type: "POST",
        url: "saveCurrenPLaylist",
        data: {
            playlist: playlist
        }
    }).done(function( msg ) {
        alert( "Data Saved: " + msg );
    });
}
*/

// Update play counter of the song
function updateNbPlayedSong(songId) {
    $.ajax({
        type: "POST",
        url: "AjaxUpdateEntitiesServlet",
        data: {
            action: "incSongPlayCount",
            songId: songId
        }
    }).done(function() {
        console.log("NB played + 1");
    });
}

// Update play counter of the playlist
function updatePlayCounterPlaylist(playlistId) {
    $.ajax({
        type: "POST",
        url: "AjaxUpdateEntitiesServlet",
        data: {
            action: "incPlaylistPlayCount",
            playlistId: playlistId
        }
    }).done(function() {
        console.log("NB played + 1");
    });
}

// Update play counter of the playlist
function updatePlayCounterAlbum(albumId) {
    $.ajax({
        type: "POST",
        url: "AjaxUpdateEntitiesServlet",
        data: {
            action: "incAlbumPlayCount",
            albumId: albumId
        }
    }).done(function() {
        console.log("NB played + 1");
    });
}

// Set the active album playlist and play
function playAlbum(albumId, playlist) {
    var obj = jQuery.parseJSON(playlist);
    console.log("Playlist chargee : " + playlist);
    projekktor('.projekktor').setFile(obj);
    projekktor('.projekktor').setPlay();
    
    updatePlayCounterAlbum(albumId);
}

// Set the active playlist and play
function playPlaylist(playlist) {
    var obj = jQuery.parseJSON(playlist);
    console.log("Playlist chargee : " + playlist);
    projekktor('.projekktor').setFile(obj);
    projekktor('.projekktor').setPlay();
}